<?php

namespace App\Admin\Controllers;

use App\User;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Models\UserGroup;
use App\Models\MoneyLog;
use Illuminate\Support\Facades\Auth;
class UserController extends Controller
{
    use ModelForm;

    protected $name = '用户管理';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');

            $grid->column('id', 'ID')->sortable();
            $grid->column('mobile', '手机号');
            $grid->column('company_name', '公司名称');
            $grid->column('company_address', '公司地址');
            $grid->column('money', '余额')->editable();
            $grid->column('company_email', '公司邮箱');
            $grid->column('contact_name', '联系人');
            $grid->column('job_name', '职位');
            
            
            $grid->column('created_at', '创建时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {

            $form->text('id', 'ID')->attribute('disabled', true);
            $form->mobile('mobile', '手机号')->rules('required|min:3');
            $form->text('company_name', '公司名称')->rules('required|min:2');
            $form->text('company_address', '公司地址')->rules('required|min:3');
            $form->text('money', '余额')->attribute('disabled', true);
            $form->email('company_email', '公司邮箱');
            $form->text('contact_name', '联系人');
            $form->text('job_name', '职位');
            
            $form->password('password', '密码');


            $form->saving(function (Form $form) {
				if ($form->money && $form->model()->money != $form->money) {
                    $MoneyLog = new MoneyLog();
					if($form->model()->money > $form->money)
					{
						$MoneyLog->content = '通过管理员操作减少金额：'.($form->model()->money-$form->money);
					}else{
						$MoneyLog->content = '通过管理员操作增加金额：'.($form->money - $form->model()->money);
					}
					$MoneyLog->user_id = $form->model()->id;
					$MoneyLog->money = ($form->money - $form->model()->money);
					$MoneyLog->save();
                }
                if ($form->user_pass && $form->model()->user_pass != $form->user_pass) {
                    $form->user_pass = bcrypt($form->user_pass);
                }
            });
        });
    }
}
